home *** CD-ROM | disk | FTP | other *** search
- Imports System.IO
- Imports System.Runtime.Serialization
- Imports System.Runtime.Serialization.Formatters.Binary
- Imports System.Runtime.Serialization.Formatters.Soap
-
- Module MainModule
-
- Sub Main()
- ' Run one of the Textxxxx procedures below by uncommenting only one statement
-
- 'TestSaveArray()
- 'TestLoadArray()
- 'TestSoapSerialization()
- 'TestSerializableClass()
- 'TestSerializableGraph()
- 'TestCloneObject()
- 'TestCustomSerialization()
- 'TestCustomSerialization2()
- 'TestIDeserializationCallback()
-
- ' These statements are usuful when running inside Visual Studio.NET
- Console.WriteLine("")
- Console.WriteLine(">>> Press Enter to terminate the program <<<")
- Console.ReadLine()
- End Sub
-
- ' this procedure tests the serialization of an array of integer
-
- Sub TestSaveArray()
- ' Create an array of integers.
- Dim arr() As Integer = {1, 2, 4, 8, 16, 32, 64, 128, 256}
-
- ' Open a file stream for output.
- Dim fs As FileStream = New FileStream("c:\powers.dat", FileMode.Create)
- ' Create a binary formatter for this stream.
- Dim bf As New BinaryFormatter()
-
- ' Serialize the array to the file stream, and flush the stream.
- bf.Serialize(fs, arr)
- fs.Close()
- End Sub
-
- ' this procedure deserializes an array of integers
-
- Sub TestLoadArray()
- ' Open a file stream for input.
- Dim fs As FileStream = New FileStream("c:\powers.dat", FileMode.Open)
- ' Create a binary formatter for this stream.
- Dim bf As New BinaryFormatter()
-
- ' Deserialize the contents of the file stream into an Integer array.
- Dim arr() As Integer
- ' Deserialize returns an object that must be coerced.
- arr = DirectCast(bf.Deserialize(fs), Integer())
-
- ' Display the result.
- Dim n As Integer
- For Each n In arr
- Console.Write(n.ToString & " ")
- Next
- Console.WriteLine("")
- End Sub
-
- ' these reusable procedure save and load data in SOAP format
-
- ' Serialize an object to file in SOAP format.
- Sub SaveSoapData(ByVal path As String, ByVal o As Object)
- ' Open a file stream for output.
- Dim fs As FileStream = New FileStream(path, FileMode.Create)
- ' Create a SOAP formatter for this stream.
- Dim sf As New SoapFormatter(Nothing, New StreamingContext(StreamingContextStates.File))
- ' Serialize the array to the file stream, and close the stream.
- sf.Serialize(fs, o)
- fs.Close()
- End Sub
-
- ' Deserialize an object from a file in SOAP format.
- Function LoadSoapData(ByVal path As String) As Object
- ' Open a file stream for input.
- Dim fs As FileStream = New FileStream(path, FileMode.Open)
- ' Create a SOAP formatter for this stream.
- Dim sf As New SoapFormatter(Nothing, New StreamingContext(StreamingContextStates.File))
-
- ' Deserialize the contents of the file stream into an object.
- LoadSoapData = sf.Deserialize(fs)
- ' close the stream.
- fs.Close()
- End Function
-
- ' this procedure tests the reusable routines above
-
- ' An example that uses the above routines.
- Sub TestSoapSerialization()
- ' Create a hashtable object and fill it with some data.
- Dim ht As New Hashtable()
- ht.Add("One", 1)
- ht.Add("Two", 2)
- ht.Add("Three", 3)
-
- ' Save the hash table to disk in SOAP format.
- SaveSoapData("c:\hashtbl.xml", ht)
-
- ' Reload the file contents into another HashTable object.
- Dim ht2 As Hashtable
- ht2 = CType(LoadSoapData("c:\hashtbl.xml"), Hashtable)
-
- ' Display values.
- Dim de As DictionaryEntry
- For Each de In ht2
- Console.WriteLine("Key=" & de.Key.ToString & _
- " Value=" & de.Value.ToString)
- Next
- End Sub
-
- ' this procedure tests the <serializable> and <notserialized> attributes
-
- Sub TestSerializableClass()
- Dim al As New ArrayList()
- al.Add(New Person("Joe", "Doe", #1/12/1960#))
- al.Add(New Person("John", "Smith", #3/6/1962#))
- al.Add(New Person("Ann", "Doe", #10/4/1965#))
-
- ' Save the ArrayList to disk in SOAP format.
- SaveSoapData("c:\hashtbl.xml", al)
-
- ' Reload the file contents into another ArrayList object.
- Dim al2 As ArrayList
- al2 = DirectCast(LoadSoapData("c:\hashtbl.xml"), ArrayList)
-
- ' Display values.
- Dim p As Person
- For Each p In al2
- Console.WriteLine(p.FirstName & " " & p.LastName & " (" & p.Age & ")")
- Next
- End Sub
-
- ' this procedure tests the serialization of an object graph
-
- Sub TestSerializableGraph()
- ' Create three Person objects.
- Dim p1 As New Person("Joe", "Doe", #1/12/1960#)
- Dim p2 As New Person("John", "Smith", #3/6/1962#)
- Dim p3 As New Person("Ann", "Doe", #10/4/1965#)
- ' Define the relationship between two of them.
- p2.Spouse = p3
- p3.Spouse = p2
-
- ' Load them into an ArrayList object.
- Dim al As New ArrayList()
- al.Add(p1)
- al.Add(p2)
- al.Add(p3)
-
- ' Save the hash table to disk in XML format
- SaveSoapData("c:\hashtbl.xml", al)
-
- ' Reload the file contents into another HashTable object.
- Dim al2 As ArrayList
- al2 = DirectCast(LoadSoapData("c:\hashtbl.xml"), ArrayList)
-
- ' Display values.
- Dim p As Person
- For Each p In al2
- Console.WriteLine(p.FirstName & " " & p.LastName & " (" & p.Age & ")")
- If Not (p.Spouse Is Nothing) Then
- ' Show the spouse's name, if there is one.
- Console.WriteLine(" Spouse of " & p.Spouse.FirstName)
- End If
- Next
- End Sub
-
- ' this procedure tests object cloning
-
- Sub TestCloneObject()
- ' create an object graph
- Dim p1 As New Person("Joe", "Doe", #1/2/1960#)
- Dim p2 As New Person("Ann", "Smith", #3/4/1965#)
- p1.Spouse = p2
- p2.Spouse = p1
-
- ' Clone it
- Dim q1 As Person = DirectCast(CloneObject(p1), Person)
- Dim q2 As Person = q1.Spouse
- ' Prove that properties were copied correctly.
- Console.WriteLine(q1.FirstName & " " & q1.LastName)
- Console.WriteLine(q2.FirstName & " " & q2.LastName)
- Console.WriteLine("P1 is Q1 = {0}", p1 Is q1)
- Console.WriteLine("P2 is Q2 = {0}", p2 Is q2)
- End Sub
-
- ' a reusable function that does object cloning
-
- Function CloneObject(ByVal obj As Object) As Object
- ' Create a memory stream and a formatter.
- Dim ms As New System.IO.MemoryStream(1000)
- Dim bf As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))
- ' Serialize the object into the stream.
- bf.Serialize(ms, obj)
- ' Position streem pointer back to first byte.
- ms.Seek(0, SeekOrigin.Begin)
- ' Deserialize into another object.
- CloneObject = bf.Deserialize(ms)
- ' release memory.
- ms.Close()
- End Function
-
- ' this procedure tests the ISerializable interface
-
- Sub TestCustomSerialization()
- ' Create a compact array of Double, whose DefaultValue is 1.
- Dim ca As New CompactDoubleArray()
- ca.DefaultValue = 1
- ' Add some elements (including some default values).
- ca.Add(1)
- ca.Add(2)
- ca.Add(3)
- ca.Add(1)
- ca.Add(4)
- ca.Add(5)
- ca.Add(1)
- ' Serialize the array.
- SaveSoapData("c:\compact.xml", ca)
-
- ' Read it back.
- Dim ca2 As CompactDoubleArray
- ca2 = DirectCast(LoadSoapData("c:\compact.xml"), CompactDoubleArray)
-
- ' Print its contents.
- Dim o As Object
- For Each o In ca2
- Console.Write(o.ToString & " ")
- Next
- Console.WriteLine("")
- End Sub
-
- ' this procedure is identical to previous one, but it uses CompactDoubleArray2
-
- Sub TestCustomSerialization2()
- ' Create a compact array of Double, whose DefaultValue is 1.
- Dim ca As New CompactDoubleArray2()
- ca.DefaultValue = 1
- ' Add some elements (including some default values).
- ca.Add(1)
- ca.Add(2)
- ca.Add(3)
- ca.Add(1)
- ca.Add(4)
- ca.Add(5)
- ca.Add(1)
-
- ' Serialize the array to a memory stream (to prove it isn't compat
- SaveSoapData("c:\compact.xml", ca)
-
- ' Read it back.
- Dim ca2 As CompactDoubleArray2
- ca2 = DirectCast(LoadSoapData("c:\compact.xml"), CompactDoubleArray2)
-
- ' Print its contents.
- Dim o As Object
- For Each o In ca2
- Console.Write(o.ToString & " ")
- Next
- Console.WriteLine("")
-
- ' Serialize the array to a memory stream (to prove it isn't compacted)
-
- ' Create the memory stream
- Dim ms As New MemoryStream(10000)
- ' Create a binary formatter for this stream
- Dim bf As New BinaryFormatter()
- ' serialize the CompactArrayDouble object to the stream
- bf.Serialize(ms, ca)
-
- ' rewind the memory stream and read its contents back into CA2
- ms.Seek(0, SeekOrigin.Begin)
- ca2 = DirectCast(bf.Deserialize(ms), CompactDoubleArray2)
- ' close the stream
- ms.Close()
-
- For Each o In ca2
- Console.Write(o.ToString & " ")
- Next
- Console.WriteLine("")
- End Sub
-
- ' this procedure tests IDeserializationCallback interface
-
- Sub TestIDeserializationCallback()
- ' Create a compact array of Point objects.
- Dim ca As New CompactPointArray()
- ' Add some elements (including some (0,0) points).
- ca.Add(New Point(1, 3))
- ca.Add(New Point(0, 0))
- ca.Add(New Point(3, 5))
- ca.Add(New Point(1, 6))
- ca.Add(New Point(0, 0))
- ca.Add(New Point(4, 8))
- ca.Add(New Point(0, 0))
- ' Serialize it.
- SaveSoapData("c:\points.xml", ca)
-
- ' Read it back.
- Dim ca2 As CompactPointArray
- ca2 = DirectCast(LoadSoapData("c:\Points.xml"), CompactPointArray)
-
- ' Print its contents.
- Dim o As Object
- For Each o In ca2
- Console.Write(o.ToString & " ")
- Next
- Console.WriteLine("")
- End Sub
-
- End Module
-